﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HZ.Public;

namespace WpfAppDemo
{
    public class RestoreData : ObservableBase
    {
        private string _FileName;

        public string FileName
        {
            get { return _FileName; }
            set { _FileName = value; }
        }

        private string _FullName;

        public string FullName
        {
            get { return _FullName; }
            set { _FullName = value; }
        }

        private string _RestoreTime;

        public string RestoreTime
        {
            get { return _RestoreTime; }
            set { _RestoreTime = value; }
        }

        private bool DeleteDB(string exec_date)
        {
            bool ret = false;
            string strSQL = "calibrationno < \"" + exec_date + "\"";
            ret = DBClass.Delete(DBClass.strCalibrationIndexTableName, strSQL);
            strSQL = "calibrationno < \"" + exec_date + "\"";
            ret = DBClass.Delete(DBClass.strCalibrationTableName, strSQL);
            strSQL = "calibrationno < \"" + exec_date + "\"";
            ret = DBClass.Delete(DBClass.strCaliFuYaIndexTableName, strSQL);
            strSQL = "StartTestTime < \"" + exec_date + "\"";
            ret = DBClass.Delete(DBClass.strDCIRTableName, strSQL);
            strSQL = "SoeTime < \"" + exec_date + "\"";
            ret = DBClass.Delete(DBClass.strNGTableName, strSQL);
            strSQL = "StartTestTime < \"" + exec_date + "\"";
            ret = DBClass.Delete(DBClass.strTestDataTableName, strSQL);
            strSQL = "Time < \"" + exec_date + "\"";
            ret = DBClass.Delete(DBClass.strWireOrderTableName, strSQL);
            return ret;
        }

        private bool importDB(string path)
        {
            bool ret = false;
            string file = path + DBClass.strCalibrationIndexTableName + ".sql";
            if (File.Exists(file))
            {
                DBClass.RestoreCopy(file);
                PublicClass.ShowProgress(30);
            }

            file = path + DBClass.strCalibrationTableName + ".sql";
            if (File.Exists(file))
            {
                DBClass.RestoreCopy(file);
                PublicClass.ShowProgress(40);
            }

            file = path + DBClass.strCaliFuYaIndexTableName + ".sql";
            if (File.Exists(file))
            {
                DBClass.RestoreCopy(file);
                PublicClass.ShowProgress(50);
            }

            file = path + DBClass.strDCIRTableName + ".sql";
            if (File.Exists(file))
            {
                DBClass.RestoreCopy(file);
                PublicClass.ShowProgress(60);
            }
            file = path + DBClass.strNGTableName + ".sql";
            if (File.Exists(file))
            {
                DBClass.RestoreCopy(file);
                PublicClass.ShowProgress(70);
            }
            file = path + DBClass.strTestDataTableName + ".sql";
            if (File.Exists(file))
            {
                DBClass.RestoreCopy(file);
                PublicClass.ShowProgress(80);
            }
            file = path + DBClass.strWireOrderTableName + ".sql";
            if (File.Exists(file))
            {
                DBClass.RestoreCopy(file);
                PublicClass.ShowProgress(98);
            }
            return ret;
        }
        private bool RestoreDB(string fullname)
        {
            string path = PublicClass.BackupDir + "temp\\";
            Directory.CreateDirectory(path);
            PublicClass.UnZip(fullname, path);
            string exe_date = "";
            exe_date = System.IO.Path.GetFileNameWithoutExtension(fullname);
            exe_date = DateTime.ParseExact(exe_date, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture).ToString("yyyy-MM-dd HH:mm:ss.zzz");
            DeleteDB(exe_date);//去重
            importDB(path);
            Directory.Delete(path, true);
            return true;
        }

        private bool RestorFile(string fullname)
        {
            PublicClass.UnZip(fullname, PublicClass.projectConfig.system.strHisDataDir);
            return true;
        }

        public bool Restore(string fullname)
        {
            bool ret = false;
            PublicClass.ShowProgressUC(PublicClass.GetLanguage("系统提示"), PublicClass.GetLanguage("正在恢复当前备份..."), true);
            new System.Threading.Thread(() =>
            {
                string path = PublicClass.BackupDir + fullname + ".fls";
                if (RestorFile(path))
                {
                    PublicClass.ShowProgress(20);
                    path = PublicClass.BackupDir + fullname + ".dat";
                    ret = RestoreDB(path);
                    PublicClass.ShowProgress(100);
                    MsgPumpClass.PublishMsg("DataQuery.RefreshTestDataList");
                }
            }).Start();
            return ret;
        }

    }
    /// <summary>
    /// UserControl3.xaml 的交互逻辑
    /// </summary>
    public partial class UserControl3 : UserControl
    {
        private ObservableCollection<RestoreData> DataList = null;

        private void LoadList()
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(PublicClass.BackupDir);

            FileInfo[] fileInfos = directoryInfo.GetFiles("*.dat", SearchOption.AllDirectories);

            foreach (var item in fileInfos)
            {
                RestoreData data = new RestoreData();
                data.FileName = System.IO.Path.GetFileNameWithoutExtension(item.FullName);
                data.FullName = item.FullName;
                data.RestoreTime = item.LastAccessTime.ToString();
                DataList.Add(data);
            }
        }
        public UserControl3()
        {
            InitializeComponent();
            DataList = new ObservableCollection<RestoreData>();
            LoadList();
            datagrid.ItemsSource = DataList;
        }
        private void Restore_Click(object sender, RoutedEventArgs e)
        {
            //int ret = PublicClass.ShowMsgBox(PublicClass.GetLanguage("将恢复当前选中的数据备份，请确认是否继续？"), true, true, PublicClass.GetLanguage("系统提示"));
            if (MessageBox.Show("将恢复当前选中的数据备份，请确认是否继续？", "系统提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            //if (ret == 1)
            {
                if (datagrid.SelectedItem != null)
                {
                    string path = DataList[datagrid.SelectedIndex].FileName;
                    DataList[datagrid.SelectedIndex].Restore(path);
                }
            }
        }
        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            //int ret = PublicClass.ShowMsgBox(PublicClass.GetLanguage("删除备份后，将无法恢复。请确认是否删除？"), true, true, PublicClass.GetLanguage("系统提示"));
            if(MessageBox.Show("删除备份后，将无法恢复。请确认是否删除？", "系统提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            //if (ret == 1)
            {
                if (datagrid.SelectedItem != null)
                {
                    string path = DataList[datagrid.SelectedIndex].FullName;
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                    path = System.IO.Path.GetDirectoryName(path) + "\\" + System.IO.Path.GetFileNameWithoutExtension(path) + ".fls";
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                    DataList.RemoveAt(datagrid.SelectedIndex);
                }
            }
        }
    }
}
